<template>
  <div>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <!-- 品牌名称 -->
      <el-form-item label="品牌名称" prop="name">
        <el-input v-model="ruleForm.name" style="width:225px;"></el-input>
      </el-form-item>
      <!-- 品牌图片 -->
      <el-form-item label="品牌图片" prop="pic_url">
        <el-upload
          class="avatar-uploader"
          action
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :http-request="uploadpic"
        >
          <img v-if="ruleForm.pic_url" :src="ruleForm.pic_url" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <!-- 品牌列表图片 -->
      <el-form-item label="品牌列表图片" prop="list_pic_url">
        <el-upload
          class="avatar-uploader"
          action
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :http-request="uploadpicList"
        >
          <img v-if="ruleForm.list_pic_url" :src="ruleForm.list_pic_url" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <!-- app专用图片 -->
      <el-form-item label="app专用图片" prop="app_list_pic_url">
        <el-upload
          class="avatar-uploader"
          action
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :http-request="uploadAppList"
        >
          <img v-if="ruleForm.app_list_pic_url" :src="ruleForm.app_list_pic_url" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <!-- 最低价 -->
      <el-form-item label="最低价" prop="floor_price">
        <el-input v-model="ruleForm.floor_price" style="width:225px;"></el-input>
      </el-form-item>
      <!-- 新品 -->
      <el-form-item label="新品" prop="is_new">
        <el-switch v-model="ruleForm.is_new"></el-switch>
      </el-form-item>
      <!-- 显示 -->
      <el-form-item label="显示" prop="is_show">
        <el-switch v-model="ruleForm.is_show"></el-switch>
      </el-form-item>
      <!-- 排序 -->
      <el-form-item label="排序" prop="sort_order">
        <el-input v-model="ruleForm.sort_order" style="width:225px;"></el-input>
      </el-form-item>
      <!-- 品牌描述 -->
      <el-form-item label="分类描述" prop="front_desc">
        <el-input type="textarea" v-model="ruleForm.front_desc" style="width:225px;"></el-input>
      </el-form-item>
      <!-- 确认保存 -->
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">确认保存</el-button>
        <el-button @click="cancle">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { PostBrandInfo, UploadImgApi } from '../../request/api'
export default {
  data() {
    return {
      catagoryList: [],
      headers: {
        'X-Nideshop-Token': localStorage.getItem('token')
      },
      ruleForm: {
        name: '',
        pic_url: '',
        list_pic_url: '',
        app_list_pic_url: '',
        floor_price: '',
        simple_desc: '',
        is_show: false,
        is_new: false,
        sort_order: '',
      },
      rules: {
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' },
        ],
        sort_order: [
          { required: true, message: '排序不能为空', trigger: 'blur' },
        ],
        front_desc: [
          { required: true, message: '品牌描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {

  },


  methods: {
    // 上传一张图片
    uploadpic(item) {
      console.log("item", item)
      let ruleForm = new FormData()
      ruleForm.append('good_pic', item.file)     //
      UploadImgApi(ruleForm).then(res => {
        console.log("返回图片路径", res.data)
        if (res.errno === 0) {
          this.ruleForm.pic_url = res.data.fileUrl
        }
      })
    },
    // 上传列表图片
    uploadpicList(item) {
      console.log("item", item)
      let ruleForm = new FormData()
      ruleForm.append('good_pic', item.file)     //
      UploadImgApi(ruleForm).then(res => {
        console.log("返回图片路径", res.data)
        if (res.errno === 0) {
          this.ruleForm.list_pic_url = res.data.fileUrl
        }
      })
    },
    // 上传app图片
    uploadAppList(item) {
      console.log("item", item)
      let ruleForm = new FormData()
      ruleForm.append('good_pic', item.file)     //
      UploadImgApi(ruleForm).then(res => {
        console.log("返回图片路径", res.data)
        if (res.errno === 0) {
          this.ruleForm.app_list_pic_url = res.data.fileUrl
        }
      })
    },
    // 商品图片
    handleAvatarSuccess(response, file, fileList) {
      //   console.log("response", response, file, fileList)
      this.pic_url = response.data.fileUrl;
    },
    beforeAvatarUpload(file, fileList) {
      //   console.log("上传文件前的钩子", file, fileList);
    },

    // 
    submitForm(formName) {
      console.log("this.ruleForm", this.ruleForm)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          PostBrandInfo({
            name: this.ruleForm.name,
            pic_url: this.ruleForm.pic_url,
            list_pic_url: this.ruleForm.list_pic_url,
            app_list_pic_url: this.ruleForm.app_list_pic_url,
            floor_price: this.ruleForm.floor_price,
            simple_desc: this.ruleForm.simple_desc,
            is_show: this.ruleForm.is_show,
            is_new: this.ruleForm.is_new,
            sort_order: this.ruleForm.sort_order,
          }).then(res => {
            console.log("提交成功", res)
            this.$router.go(-1)
          })

        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },

    // 点击取消
    cancle() {
      console.log("点击了取消")
      this.$router.go(-1)
    }

  },
  created() {

  }
}
</script>

<style lang='less' scoped>
.avatar-uploader .el-upload {
  border: 1px solid #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 105px;
  height: 105px;
  line-height: 105px;
  text-align: center;
  border: 1px solid #dcdfe6;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>